package cn.tedu.serviceEdu.controller;


import cn.tedu.commonUtils.R;
import cn.tedu.serviceEdu.entity.EduSubject;
import cn.tedu.serviceEdu.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author hong
 * @since 2023-05-17
 */
@RestController
@RequestMapping("/eduservice/subject")
@CrossOrigin
public class EduSubjectController {

    @Autowired
    private EduSubjectService subjectService;

    @PostMapping("/saveSubject")
    public R saveSubject(MultipartFile file){
        subjectService.saveObject(file,subjectService);
        return R.ok();
    }

    @GetMapping("/getSubjectTree")
    public R getSubjectTree(){
        LambdaQueryWrapper<EduSubject> lqw = new LambdaQueryWrapper<>();
        lqw.select(EduSubject::getId,EduSubject::getParentId,EduSubject::getTitle);
        List<EduSubject> subjects = subjectService.list(lqw);
        List<EduSubject> list = subjectService.getSubjectTree(subjects);
        return R.ok().data("subjectList",list);
    }

    @GetMapping("/getRootSubject")
    public R getRootSubject(){
        LambdaQueryWrapper<EduSubject> lqw = new LambdaQueryWrapper<>();
        lqw.select(EduSubject::getId,EduSubject::getParentId,EduSubject::getTitle);
        lqw.eq(EduSubject::getParentId,0);
        List<EduSubject> subjects = subjectService.list(lqw);
        return R.ok().data("items",subjects);
    }

    @GetMapping("/getChildrenSubject")
    public R getChildrenSubject(String parentId){
        LambdaQueryWrapper<EduSubject> lqw = new LambdaQueryWrapper<>();
        lqw.select(EduSubject::getId,EduSubject::getParentId,EduSubject::getTitle);
        lqw.eq(EduSubject::getParentId,parentId);
        List<EduSubject> subjects = subjectService.list(lqw);
        return R.ok().data("items",subjects);
    }

}

